<template>
  <div class="app-container">
    <el-card>
        <el-form :inline="true" class="demo-form-inline">
        <el-form-item>
          <el-input
            v-model="keyWord"
            placeholder="请输入想要查询的信息"
            @keyup.enter.native.prevent="searchCourseByName"
          ></el-input>
        </el-form-item>
        <el-form-item>
          <el-button
            type="primary"
            icon="el-icon-search"
            @click="searchCourseByName"
            >搜索</el-button
          >
        </el-form-item>
      </el-form>

      <!-- CourseTable-->
      <el-table
        v-loading="listLoading"
        :data="courses"
        element-loading-text="Loading"
        stripe
        height="550"
        border
        fits
        highlight-current-row
      >
        <el-table-column align="center" label="课程编号" prop="cId" />
        <el-table-column label="课程名称">
          <template slot-scope="{ row }">
            {{ row.cName }}
          </template>
        </el-table-column>
        <el-table-column label="授课教师" align="center">
          <template slot-scope="{ row }">
            {{ row.teacherName }}
          </template>
        </el-table-column>
        <el-table-column label="学分" align="center">
          <template slot-scope="{ row }">
            {{ row.credit }}
          </template>
        </el-table-column>

        <el-table-column label="上课地点" align="center">
          <template slot-scope="{ row }">
            {{ row.address }}
          </template>
        </el-table-column>

        <el-table-column align="center" label="课程类型">
          <template slot-scope="{ row }">
            <el-tag :type="isCourseType(row.cType)">
              {{ row.cType }}
            </el-tag>
          </template>
        </el-table-column>

        <el-table-column label="开始时间" align="center">
          <template slot-scope="{ row }">
            {{ row.startDate | dateFormater }}
          </template>
        </el-table-column>
        <el-table-column label="结束时间" align="center">
          <template slot-scope="{ row }">
            {{ row.endDate | dateFormater }}
          </template>
        </el-table-column>
        <el-table-column label="上课时间" align="center">
          <template slot-scope="{ row }">
            {{ row.classTime | timeFormater }}
          </template>
        </el-table-column>
        <el-table-column label="课时" align="center">
          <template slot-scope="{ row }">
            {{ row.courseTime }}
          </template>
        </el-table-column>
        <el-table-column label="剩余人数" align="center">
          <template slot-scope="{ row }">
            {{ row.count }}
          </template>
        </el-table-column>
        <el-table-column label="操作" align="center">
          <template slot-scope="{ row }">
            <el-button
              v-if="row.status == null"
              type="success"
              round
              size="small"
              @click="subscribe(row)"
              >报名</el-button
            >
            <el-button
              v-else-if="row.status == '未评分'"
              type="danger"
              round
              size="small"
              @click="cancel(row)"
              >取消</el-button>
              <el-tag v-else type="warning">已评分</el-tag>
          </template>
        </el-table-column>
      </el-table>
      <el-pagination layout="prev, pager, next" :total="1000"> </el-pagination>
    </el-card>
    
  </div>
</template>

<script>
import { getAllCourse, search } from "@/api/course";
import { subscribeCourse,cancelSubscribe } from "@/api/student";
import moment from "moment";

export default {
  data() {
    return {
      courses: null,
      listLoading: true,
      keyWord: "",
      dialogAddVisible: false,
    };
  },
  methods: {
    fetchData() {
      this.listLoading = true;
      getAllCourse().then((response) => {
        this.courses = response.data
        this.listLoading = false;
      });
    },
    isCourseType(type) {
      return type == "选修课" ? "success" : "danger";
    },

    subscribe(row) {
      row.status = '未评分';
      let scInfo = {
        sId:this.username,
        cId:row.cId,
        scTime: new Date(),
        status: row.status,
        score: 0
      }
      subscribeCourse(scInfo).then(resp=>{
        if(resp.status == 200){
          this.$message({
            message: resp.msg,
            type: 'success'
          })
        }
      })
    },

    cancel(row) {
      row.status = null;
      cancelSubscribe(row.cId).then(resp=>{
        if(resp.status == 200)
          this.$message({
            message: resp.msg,
            type: 'success'
          })
      })
    },

    searchCourseByName() {
      search(this.keyWord).then((resp) => {
        if (resp.status === 200) {
          this.courses = resp.data.map((v) => {
            this.$set(v, "isEdit", false);
            return v;
          });
        }
      });
    },
  },
  filters: {
    dateFormater(datetime) {
      return moment(datetime).format("YYYY-MM-DD");
    },

    timeFormater(time) {
      return moment(time).format("HH:mm:ss");
    },
  },
  computed:{
    username(){
      return this.$store.getters.name;
    }
  },
  created() {
    this.fetchData();
  },
};
</script>
<style scoped>
</style>